    //
//  CountryTableViewController.m
//  _2010WorldCupNationalAthem
//
//  Created by Apple on 2010/7/13.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "CountryTableViewController.h"
#import "athemPlayer.h"


@implementation CountryTableViewController

@synthesize listData ;
@synthesize countryNameToPlay ;
@synthesize countryNameToShow ;

//**********************************************************
// Privtate method:  setTableSelection
//  設定countey table type
// 0: group A-H,  1: round of 16,  2:quarter-Finals,  3: semi-final,  4: final,  5:champion
//**********************************************************

- (void) setTableSelection:(NSUInteger)type 
{
	tableType = type ;  // this method would be called by MainMenu
}



//**********************************************************
// Privtate method:  getCountryNameToPlay
// 根據user選到的 表格/section/row，轉換為國家名稱 (從國家名稱可以得到播放時需要的圖檔與音樂檔名)
//
//**********************************************************
-(void) setCountryNameToPlay:(NSUInteger)section rowInSection:(NSUInteger)row 
{
	//NSString * countryName = [[NSString alloc] initWithFormat:@"南非"] ;
	
	//NSString * countryName ;
	
	NSArray * array ;
	NSArray * array2 ;
	NSUInteger index ;
	
	
	switch(tableType)
	{
		case 0:	// 0: group A-H
			
			array = [[NSArray alloc] initWithObjects:
					 @"南非",	@"墨西哥",	@"烏拉圭",			@"法國",
					 @"阿根廷",	@"奈及利亞",	@"南韓",				@"希臘",
					 @"英格蘭",	@"美國",		@"阿爾及利亞",			@"斯洛維尼亞",
					 @"德國",	@"澳洲",		@"塞爾維亞",			@"迦納",
					 @"荷蘭",	@"丹麥",		@"日本",				@"喀麥隆",
					 @"義大利",	@"巴拉圭",	@"紐西蘭",			@"斯洛伐克",
					 @"巴西",	@"北韓",		@"象牙海岸",			@"葡萄牙",
					 @"西班牙",	@"瑞士",		@"宏都拉斯",			@"智利",
					 nil] ;
			array2 = [[NSArray alloc] initWithObjects:
					 @"South Africa",	@"Mexico",		@"Uruguay",			@"France",
					 @"Argentina",	@"Nigeria",		@"Korea Republic",	@"Greece",
					 @"England",		@"USA",			@"Algeria",			@"Slovenia",
					 @"Germany",		@"Australia",	@"Serbia",			@"Ghana",
					 @"Netherlands",	@"Denmark",		@"Japan",			@"Cameroon",
					 @"Italy",		@"Paraguay",	@"New Zealand",		@"Slovakia",
					 @"Brazil",		@"Korea DPR",	@"Côte d'Ivoire",	@"Portugal",
					 @"Spain",		@"Switzerland",	@"Honduras",		@"Chile",
					 nil] ;
			
			
			index = section*4 + row ;
			countryNameToPlay = [array objectAtIndex:index] ;
			countryNameToShow = [array2 objectAtIndex:index] ;
			break ;

			
		case 1:  // 1: round of 16
			array = [[NSArray alloc] initWithObjects:
					 @"烏拉圭",@"南韓",				@"墨西哥",@"阿根廷",
					 @"美國",@"迦納",								@"英格蘭",@"德國",
					 @"荷蘭",@"斯洛伐克",					@"日本",@"巴拉圭",
					 @"巴西",@"智利",							@"西班牙",@"葡萄牙",
					 nil] ;
			array2 = [[NSArray alloc] initWithObjects:
					 @"Uruguay",@"Korea Republic",				@"Mexico",@"Argentina",
					 @"USA",@"Ghana",								@"England",@"Germany",
					 @"Netherlands",@"Slovakia",					@"Japan",@"Paraguay",
					 @"Brazil",@"Chile",							@"Spain",@"Portugal",
					 nil] ;
			index = section*2 + row ;
			countryNameToPlay = [array objectAtIndex:index] ;
			countryNameToShow = [array2 objectAtIndex:index] ;
			break ;
			
		case 2:  // 2:quarter-Finals
			array = [[NSArray alloc] initWithObjects:
					 @"烏拉圭",@"迦納",            
					 @"阿根廷",@"德國",            
					 @"荷蘭",@"巴西",            
					 @"巴拉圭",@"西班牙",            
					 nil] ;
			array2 = [[NSArray alloc] initWithObjects:
					 @"Uruguay",@"Ghana",            
					 @"Argentina",@"Germany",            
					 @"Netherlands",@"Brazil",            
					 @"Paraguay",@"Spain",            
					 nil] ;
			
			index = section*2 + row ;
			countryNameToPlay = [array objectAtIndex:index] ;
			countryNameToShow = [array2 objectAtIndex:index] ;
			break ;
			
		case 3:  // 3: semi-final
			array = [[NSArray alloc] initWithObjects:
					 @"烏拉圭",@"荷蘭",            
					 @"德國",@"西班牙",            
					 nil] ;
			array2 = [[NSArray alloc] initWithObjects:
					 @"Uruguay",@"Netherlands",            
					 @"Germany",@"Spain",            
					 nil] ;

			index = section*2 + row ;
			countryNameToPlay = [array objectAtIndex:index] ;
			countryNameToShow = [array2 objectAtIndex:index] ;
			break ;
			
		case 4:  //4: final
			array = [[NSArray alloc] initWithObjects:
					 @"西班牙",@"荷蘭",            
					 @"德國",@"烏拉圭",            
					 nil] ;			
			array2 = [[NSArray alloc] initWithObjects:
					 @"Spain",@"Netherlands",            
					 @"Germany",@"Uruguay",            
					 nil] ;			

			index = section*2 + row ;
			countryNameToPlay = [array objectAtIndex:index] ;
			countryNameToShow = [array2 objectAtIndex:index] ;
			break ;
			
			
		default:  //5:champion
		case 5:
			array = [[NSArray alloc] initWithObjects:
					 @"西班牙",nil] ;			
			array2 = [[NSArray alloc] initWithObjects:
					 @"Spain",nil] ;			

			index = row ;
			countryNameToPlay = [array objectAtIndex:index] ;
			countryNameToShow = [array2 objectAtIndex:index] ;
			break ;
			
			
			
	}
	
	
	[array release] ;
	[array2 release] ;
	
	//return countryName ;		
}



//**********************************************************
// Privtate method:  jump2Player
// 跳到播放畫面，傳入國家名稱
//
//**********************************************************
-(void) jump2Player:(NSString *)countryName ;
{
	athemPlayer * tempView ;
	
	//1. create temp. player-view
	//
	//allocate a temp. country-table-view
	tempView = [[athemPlayer alloc]initWithNibName:@"athemPlayer" bundle:nil];
	
	//2. set up player.country_name
	[tempView setCountryName:countryNameToPlay nameToShow:countryNameToShow] ;
	
	//tempView.countryNameToPlay = countryName ;
	
	
	//3. jump to player 
	[self.navigationController pushViewController:tempView animated:YES] ;
	
	
	[tempView release] ;
	
}


//**********************************************************
// org. method:  viewDidLoad
//Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
//**********************************************************
- (void)viewDidLoad
{
	NSArray * array ;
	
	CGRect rect = CGRectMake(0, 0, 200, 44);
	
	UILabel * nav_bar_title ;
	
	
	// 設定navigation bar的title
	//
	nav_bar_title = [[UILabel alloc] initWithFrame:rect];
    nav_bar_title.backgroundColor = [UIColor clearColor];
    	
	
	// create NSArray for table view by "tableType"
	//
	// 0: group A-H,  1: round of 16,  2:quarter-Finals,  3: semi-final,  4: final,  5:champion
	//
	switch(tableType)
	{
		case 0:	// 0: group A-H
			
			array = [[NSArray alloc] initWithObjects:
							   @"South Africa",	@"Mexico",		@"Uruguay",			@"France",
							   @"Argentina",	@"Nigeria",		@"Korea Republic",	@"Greece",
							   @"England",		@"USA",			@"Algeria",			@"Slovenia",
							   @"Germany",		@"Australia",	@"Serbia",			@"Ghana",
							   @"Netherlands",	@"Denmark",		@"Japan",			@"Cameroon",
							   @"Italy",		@"Paraguay",	@"New Zealand",		@"Slovakia",
							   @"Brazil",		@"Korea DPR",	@"Côte d'Ivoire",	@"Portugal",
							   @"Spain",		@"Switzerland",	@"Honduras",		@"Chile",
							   nil] ;
			
			nav_bar_title.text = @"Group A-H";

			break ;

		case 1:  // 1: round of 16
			array = [[NSArray alloc] initWithObjects:
							   @"Uruguay",@"Korea Republic",				@"Mexico",@"Argentina",
							   @"USA",@"Ghana",								@"England",@"Germany",
							   @"Netherlands",@"Slovakia",					@"Japan",@"Paraguay",
							   @"Brazil",@"Chile",							@"Spain",@"Portugal",
							   nil] ;

			nav_bar_title.text = @"Round of 16";
			break ;

		case 2:  // 2:quarter-Finals
			array = [[NSArray alloc] initWithObjects:
							   @"Uruguay",@"Ghana",            
							   @"Argentina",@"Germany",            
							   @"Netherlands",@"Brazil",            
							   @"Paraguay",@"Spain",            
							   nil] ;
			
			nav_bar_title.text = @"Quarter of 16";
			break ;

		case 3:  // 3: semi-final
			array = [[NSArray alloc] initWithObjects:
					 @"Uruguay",@"Netherlands",            
					 @"Germany",@"Spain",            
					 nil] ;
			
			nav_bar_title.text = @"Semi-Final";
			break ;

		case 4:  //4: final
			array = [[NSArray alloc] initWithObjects:
					 @"Spain",@"Netherlands",            
					 @"Germany",@"Uruguay",            
					 nil] ;			

			nav_bar_title.text = @" Final";
			break ;

			
		default:  //5:champion
		case 5:
			array = [[NSArray alloc] initWithObjects:
					 @"Spain",nil] ;			
			
			nav_bar_title.text = @"Champions";
			break ;

			
			
	}
	
    self.navigationItem.titleView = nav_bar_title;
	
	
	self.listData = array ;
	
	[array release] ;
	[super viewDidLoad] ;
	
}




- (void)didReceiveMemoryWarning 
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload 
{
    [super viewDidUnload];

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	
	self.listData = nil ; 
	self.countryNameToPlay = nil ;
	self.countryNameToShow = nil ;
}


- (void)dealloc 
{
	[listData release] ;
	[countryNameToPlay release] ;
	[countryNameToShow release] ;

    [super dealloc];
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//
#pragma mark -
#pragma mark Table View Data Source Methods
//
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


//***************************************************************
// 傳回table有幾個section 
//
//***************************************************************
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
	NSInteger sectionNO ;
	
	// 0: group A-H,  1: round of 16,  2:quarter-Finals,  3: semi-final,  4: final,  5:champion
	//
	switch(tableType)
	{
		case 0:	// 0: group A-H
			sectionNO = 8 ;			
			break ;
			
		case 1:  // 1: round of 16
			sectionNO = 8 ;			
			break ;
			
		case 2:  // 2:quarter-Finals
			sectionNO = 4 ;			
			break ;
			
		case 3:  // 3: semi-final
			sectionNO = 2 ;			
			break ;
			
		case 4:  //4: final
			sectionNO = 2 ;			
			break ;
			
			
		default:  //5:champion
		case 5:
			sectionNO = 1 ;			
			break ;
			
	}
	return sectionNO ; 
	
}


//***************************************************************
// 傳回table有幾個section 
//
//***************************************************************
- (NSInteger) tableView: (UITableView *)tableView
  numberOfRowsInSection:(NSInteger)section
{
	NSInteger rowNO ;
	
	// 0: group A-H,  1: round of 16,  2:quarter-Finals,  3: semi-final,  4: final,  5:champion
	//
	switch(tableType)
	{
		case 0:	// 0: group A-H
			rowNO = 4 ;			
			break ;
			
		case 1:  // 1: round of 16
			rowNO = 2 ;			
			break ;
			
		case 2:  // 2:quarter-Finals
			rowNO = 2 ;			
			break ;
			
		case 3:  // 3: semi-final
			rowNO = 2 ;			
			break ;
			
		case 4:  //4: final
			rowNO = 2 ;			
			break ;
			
			
		default:  //5:champion
		case 5:
			rowNO = 1 ;			
			break ;
			
	}
	return rowNO ; 
	
	
}


//**********************************************************
// org. method:  tableView:cellForRowAtIndexPath
//
//**********************************************************

- (UITableViewCell *)tableView:(UITableView *)tableView
		 cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
	static NSString * SectionTableIdentifier = @"SectionTableIdentifier" ;
	NSUInteger section = [indexPath section] ;
	NSUInteger rowInSection = [indexPath row] ;
	
	NSUInteger rowInArray ;
	
	
	UITableViewCell * cell ;
	
	// 計算 section/row 在 Array 中的實際位置
	// 0: group A-H,  1: round of 16,  2:quarter-Finals,  3: semi-final,  4: final,  5:champion
	//
	switch(tableType)
	{
		case 0:	// 0: group A-H
			rowInArray = section*4 + rowInSection ;
			break ;
			
		case 1:  // 1: round of 16
		case 2:  // 2:quarter-Finals			
		case 3:  // 3: semi-final
		case 4:  //4: final
			rowInArray = section*2 + rowInSection ;			
			break ;
								
		default:  //5:champion
		case 5:
			rowInArray = rowInSection ;			
			break ;
			
	}	
	
	cell  = [tableView dequeueReusableCellWithIdentifier:SectionTableIdentifier] ;
	if(cell == nil)
	{
		cell = [[[UITableViewCell alloc]
				 initWithStyle:UITableViewCellStyleSubtitle /*Default*/ 
				 reuseIdentifier:SectionTableIdentifier] autorelease] ;
		
	}

	
	cell.textLabel.text = [listData objectAtIndex:rowInArray] ;
	cell.detailTextLabel.text = nil ;
	
	if(rowInSection == 0)
	{
		
		// 每個section的第一個元素，加上sub-title
		//
		switch(tableType)
		{
			case 0:				
			{
				NSArray * groupName = [[NSArray alloc] initWithObjects:
									   @"Group A",@"Group B",@"Group C",@"Group D",
									   @"Group E",@"Group F",@"Group G",@"Group H",nil] ;
				
				cell.detailTextLabel.text = [groupName objectAtIndex:section] ;
				
				[groupName release] ;
				
			}
				break ;
				
			case 4:
				
				if(section == 1) // 銅牌戰
				{
					cell.detailTextLabel.text = @"Match for third place" ;
				} 
				else // champions
				{
					cell.detailTextLabel.text = @"Final" ;

				}

				break ;
				
			case 5:
				cell.detailTextLabel.text = @"Champions" ;				
				break ;
				
		}
		
		
	}
	
	
	
	return cell ;
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//
#pragma mark -
#pragma mark Table View Delegate Methods
//
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



//***************************************************************
// 告知系統每個table-cell的內容 
//
//***************************************************************
- (void)tableView: (UITableView *)tableView
  didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
	NSUInteger section = [indexPath section] ;
	NSUInteger row = [indexPath row] ;
	
	
	//translate "selectedRow" to country-ID (with "tableType")
	// this method would set countryNameToPlay & countryNameToShow
	//
	[self setCountryNameToPlay:section rowInSection:row] ;	
	
	[self jump2Player:countryNameToPlay] ;
	
}


@end
